package com.example.note.cardlistview;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * a Template class for handler item to view as a common adapter in some  unified interface
 * #items : the data set for RecyclerView
 * #handler : a kind of SurfaceSlinger that transform item to view and bind data to view
 *            see inner interface SurfaceSlinger;
 * Created by Administrator on 2017/8/7 0007.
 */
public class CommonAdapter<T> extends RecyclerView.Adapter<CommonAdapter.MyViewHolder>{
    private List<T> items;
    public final  static int NOT_MATCH_TYPE = -999999;// check if whther not match
    public final  static String TYPE_NAME = "TYPE_NAME";// check if whther not match
    private List<DataHandler<T>> handlers;// handler in unified interface
    private int totalcreate = 0;
    private int totalbind =0;
    public RecycleViewBuilder recycleViewBuilder;

    public void reflesh(List<T> items){
        this.items.addAll(items);
        this.notifyDataSetChanged();
    }

    public void clear(){
        this.items.clear();
        this.notifyDataSetChanged();
    }

    /**
     * add one more handler into adapter
     * @param handler
     */
    public void addHandler(CommonAdapter.DataHandler<T> handler){
        handlers.add(handler);
    }

    /**
     * add handlers into adapter
     * @param handlers
     */
    public void addHandlers(List<DataHandler<T>> handlers){
        this.handlers.addAll(handlers);
    }

    /**
     * init function
     * @param items dataset
     * @param handlers the data Handler
     */
    public CommonAdapter(List<T> items, List<DataHandler<T>> handlers,RecycleViewBuilder recycleViewBuilder){
        this.items = items;
        this.handlers = handlers;
        this.recycleViewBuilder = recycleViewBuilder;
    }

    public CommonAdapter(List<T> items, List<DataHandler<T>> handlers){
        this.items = items;
        this.handlers = handlers;
    }
    /**
     * onCreateViewHolder
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public MyViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
        View contentView = null;
        for(CommonAdapter.DataHandler handler :handlers){
            contentView = handler.getView(parent.getContext(),viewType);
            if(contentView!=null){
                break;
            }
        }
        totalcreate++;
        Log.d("CommonAdapter","CommonAdapter:totalcreate = "+totalcreate);
        if(contentView==null){
            contentView = new TextView(parent.getContext());
        }
        return new MyViewHolder<T>(contentView);
    }

    /**
     * onBindViewHolder
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        for(CommonAdapter.DataHandler handler :handlers){
            int type = handler.getItemViewType(items.get(position));
            if(type != NOT_MATCH_TYPE){
                holder.commonAdapter=this;
                if(handler.onBindViewHolder(type,holder,items.get(position))){
                    break;
                }
            }
        }
        totalbind++;
        Log.d("CommonAdapter","CommonAdapter:totalbind = "+totalbind);
    }

    /**
     * getItemCount
     * @return
     */
    @Override
    public int getItemCount() {
        return items.size();
    }

    /**
     * getItemViewType
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        for(CommonAdapter.DataHandler handler :handlers){
            int type = handler.getItemViewType(items.get(position));
            if(type != NOT_MATCH_TYPE){
                return type;
            }
        }
        return NOT_MATCH_TYPE;
    }

    /**
     * just as name a ViewHolder
     */
    public static class MyViewHolder<T> extends RecyclerView.ViewHolder {
        public View view;
        public CommonAdapter<T> commonAdapter;
        public MyViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
        }
    }

    /**
     *  a interface that provide
     *  (1) view by viewType
     *  (2) ItemViewType by item
     *  (3) bind a item's data to screen
     * @param <T>
     */
    public interface DataHandler<T>{
        public View getView(Context context, int viewType);
        public int getItemViewType(T item);
        public boolean onBindViewHolder(int viewType, MyViewHolder holder, T item);
    }

    public void deleteItem(T Item){
        items.remove(Item);
        notifyDataSetChanged();
    }
}
